
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MemberOperation;

import CRUD.*;
import beans.MarkLurk;
import java.util.Iterator;
import java.util.Set;
import xmlMapping.*;

/**
 *
 * @author labib
 */
public class MemberThreadOperation {

    public static void RankPost(int memebrid, int postid, int degree) {
        PostHome posthome = new PostHome();
        Post post = posthome.findById(postid);
        Set setofmember = post.getMembers();

        Member vote = new Member();

        vote.setMemberId(memebrid);

        setofmember.add(vote);

        post.setMembers(setofmember);


        int oldRank = post.getRank();
        int newRank = (oldRank * setofmember.size() + degree) / setofmember.size() + 1;
        post.setRank(newRank);

        posthome.attachDirty(post);

        posthome.merge(post);


        System.out.println("   " + setofmember.size());

    }

    public static void removeLurk(MarkLurk ml) {

        ThreadHome home = new ThreadHome();
        xmlMapping.Thread thread = home.findById(ml.getId());
        Set lurkers = thread.getMembers_1();
        Member mem = new Member();
        mem.setMemberId(ml.getMemberId());

        Iterator it = lurkers.iterator();
       
        while (it.hasNext()) {
            Member meremov = (Member) it.next();
            System.out.println(" member id is" + meremov.getMemberId() + "ml.getId()" + ml.getId());
            if (meremov.getMemberId() == ml.getMemberId()) {
                lurkers.remove(meremov);
                break;
            }
        }
       
        thread.setMembers_1(lurkers);
        System.out.println(" set size after  removing is" + lurkers.size());
        home.attachDirty(thread);
    }

    public static void setThreadLurk(MarkLurk m) {
        Set Lurkers = null;
        ThreadHome home = new ThreadHome();
        xmlMapping.Thread thread = home.findById(m.getId());
        
            Lurkers = thread.getMembers_1();
            MemberHome mh=new MemberHome();
            Member mem = mh.findById(m.getMemberId());
            Lurkers.add(mem);
            thread.setMembers_1(Lurkers);
            home.attachDirty(thread);
        
    }

    public  void setThreadMark(MarkLurk m) {
        Set markers = null;
        ThreadHome home = new ThreadHome();
        xmlMapping.Thread thread = home.findById(m.getId());
        try {
            markers = thread.getMembers();
        } finally {
            Member mem = new MemberHome().findById(m.getMemberId());

            markers.add(mem);
            thread.setMembers(markers);
            home.attachDirty(thread);
        }
    }

    public  void removeThreadMark(MarkLurk m) {
        ThreadHome home = new ThreadHome();
        xmlMapping.Thread thread = home.findById(m.getId());
        System.out.println(thread.getBody());
        Set markers = thread.getMembers();
        System.out.println(markers.size());
       

        
          Iterator it = markers.iterator();
       
        while (it.hasNext()) {
            Member meremov = (Member) it.next();
          System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%"+meremov.getMemberId());
            if (meremov.getMemberId() == m.getMemberId()) {
                markers.remove(meremov);
                break;
            }
        }
        
        
       
        thread.setMembers(markers);
        home.attachDirty(thread);

    }

    public static void main(String[] args) {
        // RankPost(1, 1,3);
//
//        MarkLurk m = new MarkLurk();
//        m.setId(1); //threead id
//        m.setMemberId(4);
////        removeLurk(m);
//       setThreadMark(m);
//        MemberHome mm = new MemberHome();
//        Member user = mm.findByUserName("ali");
//        System.out.println(user.getLname());
    }
}
